package it.tommasocortonesi.scjp.varie;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

public class TestFormats {

	public static void main(String[] args) {
		TestFormats tf = new TestFormats();
		System.out.println(tf.stampaBigDecimal());

	}

	public String stampaDataFormattataIT() {
		// dato un XMLGregorianCalendar produce una stringa con la data
		// formattata
		String dataFormattata = "";
		GregorianCalendar gc = new GregorianCalendar();
		try {
			DatatypeFactory dtf = DatatypeFactory.newInstance();
			XMLGregorianCalendar xmlGC = dtf.newXMLGregorianCalendar(gc);
			Date data = new Date(xmlGC.toGregorianCalendar().getTimeInMillis());
			DateFormat df = DateFormat.getDateInstance(DateFormat.FULL,
					Locale.ITALY);
			dataFormattata = df.format(data);
		} catch (DatatypeConfigurationException e) {
			e.printStackTrace();
		}
		return dataFormattata;
	}

	public String stampaNumeroFormattato() {
		// dato un double lo formatta in denaro con due cifre decimali
		String n = "";
		double d = 101011000.3;
		NumberFormat formattatore = NumberFormat.getNumberInstance();
		formattatore = NumberFormat.getInstance(Locale.ITALY);
		formattatore.setMaximumFractionDigits(2);
		formattatore.setMinimumFractionDigits(2);
		n = formattatore.format(d);
		return n;
	}

	public String stampaBigDecimal() {
		String n = "";
		BigDecimal b = new BigDecimal("1000.3");
		BigDecimal bd = b.setScale(2, BigDecimal.ROUND_HALF_EVEN);
		return bd.toString();
	}

}
